﻿using MediatR;
using System.CJL.API.Application.Command.FinishedProductsCommand;
using System.CJL.Domain;
using System.CJL.Domain.DTO;
using System.CJL.Infrstructre.Interface;

namespace System.CJL.API.Application.CommandHanlder.FinishedProductsCommandHanler
{
	/// <summary>
	///成品显示
	/// </summary>
	public class FinishedProductsShowCommandHandler : IRequestHandler<FinishedProductsShowCommand,PageDTO<FinishedProduct>>
	{
		IFinishedProductRepositry _repositry;
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="repositry"></param>
        public FinishedProductsShowCommandHandler(IFinishedProductRepositry repositry)
        {
			_repositry=repositry;

		}
		/// <summary>
		/// 成品显示
		/// </summary>
		/// <param name="request"></param>
		/// <param name="cancellationToken"></param>
		/// <returns></returns>
		public async Task<PageDTO<FinishedProduct>> Handle(FinishedProductsShowCommand request, CancellationToken cancellationToken)
		{
			var list = (await _repositry.GetAll()).Where(x => !x.IsDelete);
			if (!string.IsNullOrEmpty(request.finishedProductNumbering))
			{
				list = list.Where(x => x.finishedProductNumbering.Contains(request.finishedProductNumbering));
			}
			if (request.finishedProductState > 0)
			{
				list = list.Where(x => x.finishedProductState == request.finishedProductState);
			}
			PageDTO<FinishedProduct> result = new PageDTO<FinishedProduct>();
			result.listcount = list.Count();
			result.pagecount = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize);
			result.plist = list.OrderByDescending(x => x.AddTime).Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize).ToList();
			return result;
		}
	}
}
